package com.cdd.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdd.index.mapper.ArticleMapper;
import com.cdd.index.service.IArticleService;
import com.common.entity.Article;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-08
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public CommonResult selectAll(String navId) {
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("nav_id", navId);
        Article article = articleMapper.selectOne(wrapper);
        if (article != null) {
            return new CommonResult(2000, "查询底层数据成功", article);
        }
        return new CommonResult(5000, "查询底层数据失败", null);
    }

    @Override
    public CommonResult updateArticle(Article article) {
        System.out.println("-------------"+article);
        if (article.getId()==null){
            article.setIsDeleted(0);
            article.setGmtModified(LocalDateTime.now());
            int insert = articleMapper.insert(article);
            if (insert!=0){
                return new CommonResult(2000,"添加成功",null);
            }
            return new CommonResult(5000,"添加失败",null);
        }
        int i = articleMapper.updateById(article);
        if (i!=0){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(2000,"修改失败",null);

    }


}
